package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.product.dto.admin.category.CategoryBindBrandDTO;
import com.chushouya.product.dto.admin.category.CategoryBrandListDTO;
import com.chushouya.product.dto.admin.category.CategoryToBindQuery;
import com.chushouya.product.service.admin.CategoryBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "商品分类关联品牌")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/categoryBrand")
public class CategoryBrandController {

    @Resource
    private CategoryBrandService categoryBrandService;

    @ApiOperation("查询商品分类待绑和已绑的品牌")
    @GetMapping("/brands")
    public Response<PageResult<CategoryBindBrandDTO>> brands(CategoryToBindQuery query) {
        return Response.success(categoryBrandService.selectCategoryToBindBrands(query));
    }

    @ApiOperation("查询商品分类已绑定的品牌")
    @GetMapping("/categoryBrands")
    public Response<List<CategoryBrandListDTO>> categoryBrands(CategoryToBindQuery query) {
        return Response.success(categoryBrandService.listCategoryBrand(query.getCategoryId(), query.getKeyword()));
    }

    @ApiOperation("绑定")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/bind")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类目id", required = true),
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true),
    })
    public Response<Long> bind(Long categoryId, Long brandId) {
        return Response.success(categoryBrandService.bind(categoryId, brandId));
    }

    @ApiOperation("修改绑定品牌排序")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/editSort")
    public Response<Long> editSort(Long bindId, Integer sort) {
        return Response.success(categoryBrandService.editSort(bindId, sort));
    }

    @ApiOperation("取消绑定")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/cancelBind")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryBrandId", value = "类目品牌id", required = true)
    })
    public Response<Void> cancelBind(Long categoryBrandId) {
        categoryBrandService.cancelBind(categoryBrandId);
        return Response.success();
    }
}
